package org.deri.socializer.activities.map;

import java.util.ArrayList;
import java.util.List;

import org.deri.socializer.NotificationPanel;
import org.deri.socializer.com.http.Surfer;

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class SurferLocationsOverlay extends ItemizedOverlay<OverlayItem> {
 
	private List<SurferOverlayItem> items;
	private Drawable marker;
	private GoogleMapsActivity googleMapsActivity;
	private NotificationPanel popupPanel;
	
	
	public SurferLocationsOverlay(Drawable defaultMarker, GoogleMapsActivity googleMapsActivity) {
		super(defaultMarker);
		items = new ArrayList<SurferOverlayItem>();
		marker = defaultMarker;
		this.googleMapsActivity = googleMapsActivity;
		this.popupPanel = googleMapsActivity.getPopupPanel();
		populate();
	}
 
	protected OverlayItem createItem(int index) {
		return (OverlayItem)items.get(index);
	}
 
	public int size() {
		return items.size();
	}
 
	public void clear() {
		this.items.clear();
		setLastFocusedIndex(-1);
		populate();
	}
	
	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.google.android.maps.ItemizedOverlay#draw(android.graphics.Canvas,
	 * com.google.android.maps.MapView, boolean)
	 */
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
		boundCenterBottom(marker);
	}
 
	public void addItem(SurferOverlayItem surferItem) {
		items.add(surferItem);
		setLastFocusedIndex(-1);
		populate();
	}
 
	
	@Override
	protected boolean onTap(int index) {
	    SurferOverlayItem item = this.items.get(index);
	    MapView map = this.googleMapsActivity.getMap();
	    
	    this.popupPanel.hide();
	      
	    GeoPoint geo = item.getPoint();
	    Point pt = map.getProjection().toPixels(geo, null);
	    
	    final Surfer surfer = item.getSurfer();

	    boolean alignTop = pt.y*2>map.getHeight();
	    //this.popupPanel.show(pt.y*2>map.getHeight());
	    this.popupPanel.showSurferActionNotification(this.googleMapsActivity, this.googleMapsActivity.getXmppClient(), surfer, alignTop);
	    return true;
	 }


}